package org.linlinjava.litemall.wx.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.db.dao.LitemallArrivalReminderMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallGoodsProductService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author piao
 * @Date 2021/03/19
 */
@Service
public class WxArrivalReminderService {
    @Autowired
    private LitemallArrivalReminderMapper arrivalReminderMapper;
    @Autowired
    private LitemallGoodsProductService goodsProductService;
    @Autowired
    private LitemallGoodsService goodsService;
    @Autowired
    private LitemallUserService userService;

    /**
     * 创建到货提醒
     *
     * @param userId
     * @param productId
     */
    public void create(Integer userId, Integer productId) {
        LitemallUser user = userService.findById(userId);
        LitemallGoodsProduct goodsProduct = goodsProductService.findById(productId);
        if (goodsProduct == null) {
            return;
        }
        LitemallArrivalReminder litemallArrivalReminder = findByUserIdAndProductId(userId, productId);
        if (litemallArrivalReminder != null) {
            return;
        }
        LitemallGoods goods = goodsService.findById(goodsProduct.getGoodsId());
        LitemallArrivalReminder arrivalReminder = new LitemallArrivalReminder();
        arrivalReminder.setUserId(userId);
        arrivalReminder.setNickname(user.getNickname());
        arrivalReminder.setProductId(productId);
        arrivalReminder.setSpecifications(goodsProduct.getSpecifications());
        arrivalReminder.setBookName(goods.getName());
        arrivalReminder.setPicUrl(goods.getPicUrl());
        arrivalReminder.setStatus(1);
        arrivalReminder.setAddTime(LocalDateTime.now());
        arrivalReminder.setUpdateTime(LocalDateTime.now());
        arrivalReminderMapper.insertSelective(arrivalReminder);
    }

    public LitemallArrivalReminder findByUserIdAndProductId(Integer userId, Integer productId) {
        LitemallArrivalReminderExample example = new LitemallArrivalReminderExample();
        example.or().andUserIdEqualTo(userId).andProductIdEqualTo(productId);
        return arrivalReminderMapper.selectOneByExample(example);
    }


}
